//
//  MainTabBarController.m
//  MXClassroom
//
//  Created by 你个LB on 16/9/8.
//  Copyright © 2016年 你个LB. All rights reserved.
//

#import "MainTabBarController.h"

@interface MainTabBarController ()
{
    UIImageView *imageView;
    NSArray *imageArray;
}
@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    imageArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"startImg0"],[UIImage imageNamed:@"startImg1"],[UIImage imageNamed:@"startImg2"],[UIImage imageNamed:@"startImg3"],[UIImage imageNamed:@"startImg4"],[UIImage imageNamed:@"startImg5"],[UIImage imageNamed:@"startImg6"], nil];
    
    DiscoverViewController * discoverVC = [[DiscoverViewController alloc] init];
    discoverVC.tabBarItem.title = @"发现";
    discoverVC.tabBarItem.image = [UIImage imageNamed:@"nav_discover50px@2x.png"];
    [discoverVC.tabBarItem setSelectedImage:[UIImage imageNamed:@"nav_discover_down50px@2x.png"]];
    
    SubscribeViewController * subscribeVC = [[SubscribeViewController alloc] init];
    subscribeVC.tabBarItem.title = @"订阅";
    subscribeVC.tabBarItem.image = [UIImage imageNamed:@"nav_subscribe50px@2x.png"];
    [subscribeVC.tabBarItem setSelectedImage:[UIImage imageNamed:@"nav_subscribe_down50px@2x.png"]];
    
    ZenViewController * zenVC = [[ZenViewController alloc] init];
    [self addCenterButtonWithImage:[UIImage imageNamed:@"佛学科学150px@3x.png"] highlightImage:nil];
    
    DownloadViewController * downloadVC = [[DownloadViewController alloc] init];
    downloadVC.tabBarItem.title = @"下载";
    downloadVC.tabBarItem.image = [UIImage imageNamed:@"nav_down50px@2x.png"];
    [downloadVC.tabBarItem setSelectedImage:[UIImage imageNamed:@"nav_down_down50px@2x.png"]];
    
    MyViewController * myVC = [[MyViewController alloc] init];
    myVC.tabBarItem.title = @"我的";
    myVC.tabBarItem.image = [UIImage imageNamed:@"nav_my50px@2x.png"];
    [myVC.tabBarItem setSelectedImage:[UIImage imageNamed:@"nav_my_down50px@2x.png"]];
    
//    self.viewControllers = @[discoverVC,subscribeVC,zenVC,downloadVC,myVC];
     // 把所有的视图控制器存放在数组中
    NSArray *viewControllers = @[discoverVC, subscribeVC, zenVC, downloadVC, myVC];
    
    // 便利为每一个视图控制器创建导航控制器并存放到数组中
    NSMutableArray *navCtrls = [NSMutableArray array];
    for (UIViewController *viewController in viewControllers) {
        // 创建导航视图控制器
        BaseNavigationController *baseNavCtrl = [[BaseNavigationController alloc] initWithRootViewController:viewController];
        // 把导航视图控制器存放到数组中
        [navCtrls addObject:baseNavCtrl];
    }
    // 把所有的导航视图控制器作为标签控制器的子视图控制器
    self.viewControllers = navCtrls;
}

//添加中间按钮
- (void)addCenterButtonWithImage:(UIImage *)buttonImage highlightImage:(UIImage *)highlightImage
{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
    
    CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
    if (heightDifference < 0)
        button.center = self.tabBar.center;
    else
    {
        CGPoint center = self.tabBar.center;
        center.y = center.y - heightDifference/2.0;
        button.center = center;
    }
    
    [self.view addSubview:button];
}

- (void)buttonClick
{
    NSLog(@"点击中间按钮");
    
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    imageView.animationImages = imageArray;
    imageView.animationRepeatCount = 0;
    imageView.animationDuration = 1.0f;
    [imageView startAnimating];
    [self.view addSubview:imageView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end




//
//
//// 2.定义标签栏样式
//- (void)_initCustomTabBar
//{
//    // 1.设置标签栏为不透明
//    self.tabBar.translucent = NO;
//    
//    // 2.设置标签栏标题文本和标题图片
//    NSArray *titles = @[@"发现",@"订阅",@"下载",@"个人"];
//    NSArray *imageNames = @[@"导航_发现75px.png",@"导航_订阅75px.png",@"导航_下载75px.png",@"75px.png"];
//    NSArray *selectImageNames = @[@"导航_发现点击75px.png",@"导航_订阅点击75px.png",@"导航_下载点击75px.png",@"导航_个人点击75px.png"];;
//    for (int i = 0; i < titles.count; i++) {
//        // 01 获取对应的导航视图控制器
//        UINavigationController *navCtrl = self.viewControllers[i];
//        // 02 设置视图控制器的标题
//        navCtrl.tabBarItem.title = titles[i];
//        // 03 设置导航视图控制器的标签栏内容
//        navCtrl.tabBarItem.image = [UIImage imageNamed:imageNames[i]];
//        UIImage *selectedImage = [UIImage imageNamed:selectImageNames[i]];
//        // 取消图片被自动改变渲染方法
//        navCtrl.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    }
//    
//    // 3.设置标签栏选中图片颜色31 38 132
//    self.tabBar.selectedImageTintColor = [UIColor colorWithRed:26 /255.0 green:145 /255.0 blue:210/255.0 alpha:1.0];
//    
//    // 4.设置标签栏背景颜色
//    self.tabBar.barTintColor = [UIColor whiteColor];
//}
//
//// 1.初始化子视图控制器
//- (void)_initViewControllers
//{
//    // 01 创建发现视图控制器
//    DiscoverViewController * discoverVC = [[DiscoverViewController alloc] init];
//    
//    // 02 创建订阅视图控制器
//    SubscribeViewController * subscribeVC = [[SubscribeViewController alloc] init];
//    
//    // 03 创建下载视图控制器
//    DownloadViewController * downloadVC = [[DownloadViewController alloc] init];
//    
//    // 04 创建隔个人视图控制器
//    MyViewController * myVC = [[MyViewController alloc] init];
//    
//    // 05 把所有的视图控制器存放在数组中
//    NSArray *viewControllers = @[discoverVC, subscribeVC, downloadVC, myVC];
//    
//    // 06 便利为每一个视图控制器创建导航控制器并存放到数组中
//    NSMutableArray *navCtrls = [NSMutableArray array];
//    for (UIViewController *viewController in viewControllers) {
//        // 创建导航视图控制器
//        BaseNavigationController *baseNavCtrl = [[BaseNavigationController alloc] initWithRootViewController:viewController];
//        // 把导航视图控制器存放到数组中
//        [navCtrls addObject:baseNavCtrl];
//    }
//    // 07 把所有的导航视图控制器作为标签控制器的子视图控制器
//    self.viewControllers = navCtrls;
//    
//}
//
//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}
//
//@end
